const HomeText = require('../models/homeText')

class TextCtl {
  async findList(ctx) {
    ctx.body = await HomeText.find({
      state: '1'
    })
  }
  async findById(ctx) {
    ctx.body = ctx.state.homeText
  }
  async create(ctx) {
    ctx.verifyParams({
      name: { type: 'string', required: true },
      state: { type: 'string', required: false }
    })
    const option = await new HomeText({
      ...ctx.request.body
    }).save()
    ctx.body = option
  }
  async update(ctx) {
    ctx.verifyParams({
      state: { type: 'string', required: false }
    })
    await ctx.state.homeText.update(ctx.request.body)
    ctx.body = ctx.state.homeText
  }
  async checkTextExist(ctx, next) { //检查是否存在
    const id = ctx.params.id
    const text = await HomeText.findById(id)
    if (text) {
      ctx.state.homeText = text
      await next()
    } else {
      ctx.throw(404, '该选项不存在')
    }
  }
  async delete(ctx) {
    await ctx.state.homeText.set({ state: '0' }).save()
    ctx.status = 204
  }
}

module.exports = new TextCtl